package who.tmap.toolkit.cdr.impl;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Point2D;

import who.tmap.bo.cd.CDPointSymbols;
import who.tmap.bo.cd.CartographicDisplay;
import who.tmap.common.TMapException;
import who.tmap.toolkit.MappingToolKit;
import who.tmap.toolkit.cdr.CDRPoint;
import who.tmap.toolkit.shape.MapShape;
import who.tmap.toolkit.shape.MapShapes;
import who.tmap.toolkit.shape.annotation.impl.ImageHelper;
import who.tmap.toolkit.shape.cdpointsymbol.CDPointSymbol;
import who.tmap.toolkit.shape.cdpointsymbol.ImageCDPointSymbol;

public abstract class BaseCDRPointImpl extends BaseCDRImpl implements CDRPoint {
	
	protected double maxVal =Double.MIN_VALUE;
	protected ImageHelper imageHelper =null;
	
	public void init( MappingToolKit mtk, CartographicDisplay cd, double mapFactor ) throws TMapException {
		super.init( mtk, cd, mapFactor );
	}
	
	public void represent(Graphics2D g, MapShape mapShape) throws TMapException {
		throw new TMapException( getClass().getName() + " does not implement this method!" );
	}	
	
	protected CDPointSymbol getShapeForSymbol( int symbol ) throws TMapException {
		CDPointSymbol shape =null;
		
		if ( symbol == CDPointSymbols.CIRCLE ) {
			shape =(CDPointSymbol)mtk.addShape( MapShapes.CDPOINT_CIRCLE );
		} else if ( symbol == CDPointSymbols.TRIANGLE ) {
			shape =(CDPointSymbol)mtk.addShape( MapShapes.CDPOINT_TRIANGLE );
		} else if ( symbol == CDPointSymbols.CROSS ) {
			shape =(CDPointSymbol)mtk.addShape( MapShapes.CDPOINT_CROSS );
		} else if ( symbol == CDPointSymbols.SQUARE ) {
			shape =(CDPointSymbol)mtk.addShape( MapShapes.CDPOINT_SQUARE );
		} else if ( symbol == CDPointSymbols.BITMAP ) {
			shape =(CDPointSymbol)mtk.addShape( MapShapes.CDPOINT_IMAGE );
		}
		
		return shape;
	}
	
	/**
	 * radius of circle = ((data value)/(maximum data value))^0.57 x radius of maximum circle to be drawn.
	 *
	 * Source: Dent, Borden D. Cartography: Thematic Map Design, 2d edi.Wm. .C. Brown, Publishers, 1990.
	 * @param mapShape
	 * @param scalingFactor
	 * @return
	 */
	protected double computeSymbolSize( double val, int scalingFactor ) {
		double sfSize =0, size =0, sizePC =0;

		if ( val != java.lang.Double.MIN_VALUE ) {

			if ( maxVal != 0 ) {
				sizePC =Math.pow( val / maxVal, 0.57 );
			} else {
				sizePC =0;
			}
			
			if ( sizePC != 0 ) {
				size=((double)scalingFactor * sizePC) / mapFactor;				
			} else {
				size =0;
			}			
			
			sfSize =Math.abs( size );
		} 
		
		//logger.debug( "val=" + val + ", sizePC=" + sizePC + ", size=" + size + ", sfSize=" + sfSize );
		
		return sfSize;
	}		
	
	protected void drawSymbol( Graphics2D g, Point2D pt, boolean selected, Color color, int fillPattern, double sizeVal, int symbol ) throws TMapException {
		CDPointSymbol shape =getShapeForSymbol( symbol );
		
		shape.setPt( pt );
		shape.setColor( color );
		shape.setFillPattern( fillPattern );
		
		shape.setWidth( sizeVal );
		shape.setHeight( sizeVal );
		
		shape.select( selected );
		if ( selected ) {
			mtk.getSelectedShapes().add( shape );
		}
		shape.setMapFactor( mapFactor );
		shape.setZoomFactor( mtk.getZoomFactor() );
		
		shape.draw( g );
	}	
	
	protected void drawBitmap(Graphics2D g, Point2D pt, boolean selected, double symbolSize, Image img ) throws TMapException {//, File file ) throws TMapException {
		ImageCDPointSymbol shape =(ImageCDPointSymbol)getShapeForSymbol( CDPointSymbols.BITMAP );
		shape.setImage( img );
		shape.setPt( pt );
		
		shape.setWidth( symbolSize * mapFactor );
		shape.setHeight( symbolSize * mapFactor );
		
		shape.select( selected );
		if ( selected ) {
			mtk.getSelectedShapes().add( shape );
		}
		shape.setMapFactor( mapFactor );
		shape.setZoomFactor( mtk.getZoomFactor() );
		
		shape.draw( g );
	}

}
